'use strict';
var o = Object.defineProperty;
var g = (c, t, e) =>
  t in c
    ? o(c, t, { enumerable: !0, configurable: !0, writable: !0, value: e })
    : (c[t] = e);
var r = (c, t, e) => (g(c, typeof t != 'symbol' ? t + '' : t, e), e);
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const h = require('../nodes/identity.cjs'),
  m = require('../visit.cjs'),
  d = {
    '!': '%21',
    ',': '%2C',
    '[': '%5B',
    ']': '%5D',
    '{': '%7B',
    '}': '%7D',
  },
  p = c => c.replace(/[!,[\]{}]/g, t => d[t]),
  n = class n {
    constructor(t, e) {
      r(this, 'yaml');
      r(this, 'tags');
      r(this, 'docStart', null);
      r(this, 'docEnd', !1);
      r(this, 'atNextDocument');
      (this.yaml = Object.assign({}, n.defaultYaml, t)),
        (this.tags = Object.assign({}, n.defaultTags, e));
    }
    clone() {
      const t = new n(this.yaml, this.tags);
      return (t.docStart = this.docStart), t;
    }
    atDocument() {
      const t = new n(this.yaml, this.tags);
      switch (this.yaml.version) {
        case '1.1':
          this.atNextDocument = !0;
          break;
        case '1.2':
          (this.atNextDocument = !1),
            (this.yaml = { explicit: n.defaultYaml.explicit, version: '1.2' }),
            (this.tags = Object.assign({}, n.defaultTags));
          break;
      }
      return t;
    }
    add(t, e) {
      this.atNextDocument &&
        ((this.yaml = { explicit: n.defaultYaml.explicit, version: '1.1' }),
        (this.tags = Object.assign({}, n.defaultTags)),
        (this.atNextDocument = !1));
      const i = t.trim().split(/[ \t]+/),
        l = i.shift();
      switch (l) {
        case '%TAG': {
          if (
            i.length !== 2 &&
            (e(0, '%TAG directive should contain exactly two parts'),
            i.length < 2)
          )
            return !1;
          const [s, a] = i;
          return (this.tags[s] = a), !0;
        }
        case '%YAML': {
          if (((this.yaml.explicit = !0), i.length !== 1))
            return e(0, '%YAML directive should contain exactly one part'), !1;
          const [s] = i;
          if (s === '1.1' || s === '1.2') return (this.yaml.version = s), !0;
          {
            const a = /^\d+\.\d+$/.test(s);
            return e(6, `Unsupported YAML version ${s}`, a), !1;
          }
        }
        default:
          return e(0, `Unknown directive ${l}`, !0), !1;
      }
    }
    tagName(t, e) {
      if (t === '!') return '!';
      if (t[0] !== '!') return e(`Not a valid tag: ${t}`), null;
      if (t[1] === '<') {
        const a = t.slice(2, -1);
        return a === '!' || a === '!!'
          ? (e(`Verbatim tags aren't resolved, so ${t} is invalid.`), null)
          : (t[t.length - 1] !== '>' && e('Verbatim tags must end with a >'),
            a);
      }
      const [, i, l] = t.match(/^(.*!)([^!]*)$/);
      l || e(`The ${t} tag has no suffix`);
      const s = this.tags[i];
      if (s)
        try {
          return s + decodeURIComponent(l);
        } catch (a) {
          return e(String(a)), null;
        }
      return i === '!' ? t : (e(`Could not resolve tag: ${t}`), null);
    }
    tagString(t) {
      for (const [e, i] of Object.entries(this.tags))
        if (t.startsWith(i)) return e + p(t.substring(i.length));
      return t[0] === '!' ? t : `!<${t}>`;
    }
    toString(t) {
      const e = this.yaml.explicit
          ? [`%YAML ${this.yaml.version || '1.2'}`]
          : [],
        i = Object.entries(this.tags);
      let l;
      if (t && i.length > 0 && h.isNode(t.contents)) {
        const s = {};
        m.visit(t.contents, (a, u) => {
          h.isNode(u) && u.tag && (s[u.tag] = !0);
        }),
          (l = Object.keys(s));
      } else l = [];
      for (const [s, a] of i)
        (s === '!!' && a === 'tag:yaml.org,2002:') ||
          ((!t || l.some(u => u.startsWith(a))) && e.push(`%TAG ${s} ${a}`));
      return e.join(`
`);
    }
  };
r(n, 'defaultYaml', { explicit: !1, version: '1.2' }),
  r(n, 'defaultTags', { '!!': 'tag:yaml.org,2002:' });
let f = n;
exports.Directives = f;
